<!-- File: /app/View/Acounts/assign_pos.ctp -->

<!-- @author karim kamel -->


<html>
    <?php
    $this->layout = 'admin';
    echo $this->Html->script('DragDropHelpers'); // Include jQuery library
    echo $this->Html->script('EventHelpers'); // Include jQuery library
    echo $this->Html->script('permissionForm'); // Include jQuery library
    echo $this->Html->css('permission'); // Include css library
    ?>
    <script type="text/javascript">
        function assign() //takes the lists of names and put each list in an array and pass it to the controller
        {
            var links_list1 = [];
            var links1 = document.getElementById('moderatorUsers').getElementsByTagName('a');
            for(var a in links1) {
                if(typeof links1[a] == undefined) continue;
                links_list1.push(links1[a].innerHTML);} 
            var moderatorss =links_list1.toString();
            var moderators = moderatorss.split(',');
            var moderators1 = [];
            for(i=0;i<moderators.length;i++){
                if (moderators[i]!=""){
                    moderators1[i]=moderators[i];                  
                }}
            
            var links_list2 = [];
            var links2 = document.getElementById('editorUsers').getElementsByTagName('a');
            for(var a in links2) {
                if(typeof links2[a] == undefined) continue;
                links_list2.push(links2[a].innerHTML);} 
            //alert(links_list2);
            var editorss =links_list2.toString();
            var editors = editorss.split(',');
            var editors1 = [];
            for(i=0;i<editors.length;i++){
                if (editors[i]!=""){
                    editors1[i]=editors[i];                  
                }}
             
            var links_list3 = [];
            var links3 = document.getElementById('jEditorUsers').getElementsByTagName('a');
            for(var a in links3) {
                if(typeof links3[a] == undefined) continue;
                links_list3.push(links3[a].innerHTML);} 
            //alert(links_list3);
            var jEditorss =links_list3.toString();
            var jEditors = jEditorss.split(',');
            var jEditors1 = [];
            for(i=0;i<jEditors.length;i++){
                if (jEditors[i]!=""){
                    jEditors1[i]=jEditors[i];                 
                }}
            if(moderators1.length == 0 && editors1.length == 0 && jEditors1.length== 0){
                moderators1[0] = "empty";
                editors1[0] = "empty";
                jEditors1[0] = "empty";
            }
                                
            $.post("/Accounts/assign_pos", 
            { moderators1: moderators1, 
                editors1: editors1, 
                jEditors1: jEditors1
                
            });
			
			alert("Changes Saved");
            window.location.replace("/Accounts/assign_pos");
        }
    </script>
    <body>
        <?php if (!empty($admin) && $admin == 1) { ?>
		<?php $this->start('leftmenu'); ?>
        <div class="well pull-left">
            <ul class="nav nav-list">
                <li><?php echo $this->Html->link('Manage Accounts', array('controller' => 'tattlraccounts', 'action' => 'index')); ?></li>
                <li><a href="/tattlraccounts/addaccount">Add Account</a></li>
                <li><?php echo $this->Html->link('Assign Positions', array('controller' => 'Accounts', 'action' => 'assign_pos')); ?></li>
            </ul>
        </div>
            <?php $this->end(); ?>
            <form>
                <fieldset> 
                    <legend>Roles Assigning ( <?php if(!empty($name)) {echo $name; echo ' )';}?></legend> 

                    <p>The chart below shows all the
                        users in the system. To assign a role to a user,
                        drag and drop the user into the right
                        box.</p>

                    <table class="userTable">
                        <thead>
                            <tr>
                                <th>Unassigned Users</th>

                                <th>Moderators</th>

                                <th>Editors</th>

                                <th>JEditors</th>
                            </tr>
                        </thead>

                        <tbody>
                            <tr>
                                <td id="unassignedUsers" class="userList"> <!-- inserts the unAssigned users -->
                                    <?php if (!empty($arrayusers)) {
                                        for ($i = 0; $i < $length; $i++) { ?>
                                            <a href="#" draggable="true"><?php echo $arrayusers[$i]['users']['username']; ?> </a>
                                            <?php
                                        }
                                    } else {
                                        
                                    }
                                    ?>
                                </td>

                                <td id="moderatorUsers" class="userList"><!-- inserts the Moderators -->
                                    <?php if (!empty($mod)) {
                                        for ($i = 0; $i < $modlen; $i++) { ?>
                                            <a href="#" draggable="true"><?php echo $mod[$i]['users']['username']; ?> </a>
                                            <?php
                                        }
                                    } else {
                                        
                                    }
                                    ?> 
                                </td>

                                <td id="editorUsers" class="userList"><!-- inserts the Editors -->
                                    <?php if (!empty($edi)) {
                                        for ($i = 0; $i < $edilen; $i++) { ?>
                                            <a href="#" draggable="true"><?php echo $edi[$i]['users']['username']; ?> </a>
                                            <?php
                                        }
                                    } else {
                                        
                                    }
                                    ?> 
                                </td>

                                <td id="jEditorUsers" class="userList"><!-- inserts the JEditors -->
                                    <?php if (!empty($jEdi)) {
                                        for ($i = 0; $i < $jEdilen; $i++) { ?>
                                            <a href="#" draggable="true"><?php echo $jEdi[$i]['users']['username']; ?> </a>
                                            <?php
                                        }
                                    } else {
                                        
                                    }
                                    ?> 
                                </td> 
                            </tr>
                        </tbody>
                        <tfoot>
                        <td id="unassignedUsersHelp">Drag a user from this list to another list to 
                            assign him his role.</td>
                        <td id="moderatorUsersHelp">Drag user here</td>
                        <td id="editorUsersHelp" >Drag user here</td>
                        <td id="jEditorUsersHelp" >Drag user here</td>
                        </tfoot>
                    </table>
                </fieldset> 
                <h1 style="display:none;"></h1>
                <button  class ="button" onClick="assign()">Save Changes</button>
            </form>

        <?php } ?>
    </body>


